package com.ftg.midamall.admin.service.impl;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ftg.midamall.admin.dto.UmsMenuVoo;
import com.ftg.midamall.admin.entity.UmsMenu;
import com.ftg.midamall.admin.mapper.UmsMenuMapper;
import com.ftg.midamall.admin.service.IUmsMenuService;
import com.ftg.midamall.admin.vo.UmsMenuTreeVo;
import com.ftg.midamall.admin.dto.UmsMenuVo;
import com.ftg.midamall.comm.tool.ConventUtil;
import com.ftg.midamall.comm.tool.TokenUtil;
import com.ftg.midamall.comm.vo.CodeStatusEnum;
import com.ftg.midamall.comm.vo.RespMessUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 后台用户表 服务实现类
 * </p>
 *
 * @author qjw
 * @since 2023-10-20
 */
@Service
public class UmsMenuServiceImpl extends ServiceImpl<UmsMenuMapper, UmsMenu> implements IUmsMenuService {

    @Autowired
    UmsMenuMapper umsMenuMapper;
    @Override
    public RespMessUtil<List<UmsMenuTreeVo>> showMenuByAdminId(String token) {

        System.out.println(token);
        JSONObject jsonObject = TokenUtil.parseTokenWithObject(token);

        Long adminId = jsonObject.getLong("id");
        System.out.println(adminId);
        System.out.println(jsonObject);

        //取数据库里面的数据
        List<UmsMenu> menus = this.getBaseMapper().showMenuByAdminId(adminId);

        //所数据转换成树形结构
        List<UmsMenuTreeVo> tree = ConventUtil.createTree(menus, UmsMenuTreeVo.class);

        return RespMessUtil.build(CodeStatusEnum.SUCCESS,tree);
    }

    /**
     * 展示菜单
     * @return
     */
    @Override
    public RespMessUtil<List<UmsMenuTreeVo>> allMenuForTree() {

        List<UmsMenu> all = this.getBaseMapper().selectList(null);

        if(Objects.nonNull(all)){
            List<UmsMenuTreeVo> tree = ConventUtil.createTree(all, UmsMenuTreeVo.class);
            return RespMessUtil.build(CodeStatusEnum.SUCCESS,tree);
        }
        return RespMessUtil.build(CodeStatusEnum.FAILURE);
    }



    public int insertmenu(UmsMenuVo ge) {
//        if (!Objects.isNull(umsMenuMapper.selectmenuByname(ge.getName()))) {
//           throw new RuntimeException("商品名已经存在");
//        }
//        if (!Objects.isNull(umsMenuMapper.selectmenuByid(ge.getId()))) {
//            throw new RuntimeException("商品id已经存在");
//        }

        System.out.println("ssss");
        return umsMenuMapper.addmenu(ge);
    }
    public int deletemenu(int id ){
        System.out.println("该菜单下有角色，请谨慎删除");
        if(Objects.isNull(umsMenuMapper.selectmenuByid(id))){
            throw new RuntimeException("此商品不存在");
        }else{
        return umsMenuMapper.deletemenu(id);
    }}


    public RespMessUtil<IPage> showAllWithPage(long pageSize, long pageLimit, String MenuName) {
        IPage<UmsMenu> ipage=new Page<>(pageSize,pageLimit);
        QueryWrapper<UmsMenu> query =new QueryWrapper<>();
        if(!StrUtil.isBlank(MenuName)){
            query.like("name",MenuName);
        }

        return RespMessUtil.build(CodeStatusEnum.SUCCESS,this.getBaseMapper().selectPage(ipage,query));
    }


    public int updatemenu(UmsMenuVo se ){
        int up= umsMenuMapper.updatemenu(se,se.getId());
        if (up == 0) {
            return 0;
        }
        return 1;
    }

    public int deletemenus(List<Integer> ids) {
        int i = umsMenuMapper.deleteBatchIds(ids);return i;

    }
    }

